﻿/* Project: Side Scrolling Shooter
 * The explosion caused by the bomb's death.
 * Detects if it has collided with an enemy.
 * Last Update: 4/12/11,11:16PM
 */

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace TheFifthSpace
{
    public class BombExplosion : DrawableGameComponent
    {
        public float posX, posY;
        public Texture2D sprite;
        //private SpriteBatch spriteBatch;
        private Vector2 offset;
        private const int x = 320;
        private const int y = 280;
        private const int fps_Max = 20;
        public Point currentFrame;
        private int frameStart;
        int updateCount;

        public Vector2 Position
        {
            get
            {
                return new Vector2(posX, posY);
            }
            set
            {
                posX = value.X;
                posY = value.Y;
            }
        }

        // CONSTRUCTOR
        public BombExplosion(Game g, Vector2 p)
            : base(g)
        {
            Game.Components.Add(this);
            Position = p;
            currentFrame = new Point(0, 0);
            frameStart = 0;
            updateCount = 0;
        }

        // LOAD CONTENT
        protected override void LoadContent()
        {
            sprite = MainGame.bombExplosionSprite;
            //spriteBatch = MainGame.spriteBatch;
            offset = new Vector2(x / 2, y / 2);
        }

        // DRAW
        public override void Draw(GameTime gameTime)
        {
            // Make sure animation fps is no faster than game fps
            frameStart += gameTime.ElapsedGameTime.Milliseconds;
            if (frameStart >= fps_Max) frameStart -= fps_Max;

            MainGame.spriteBatch.Begin();
            MainGame.spriteBatch.Draw(sprite, Position - offset,
                new Rectangle(currentFrame.X * 320, currentFrame.Y * 240, x, y), Color.White);
            MainGame.spriteBatch.End();
        }

        public override void Update(GameTime gameTime)
        {
            updateCount++;
            if (updateCount > 12)
            {
                currentFrame.X++; // move the source rectangle around spritesheet
                if (currentFrame.X >= 4)
                {
                    currentFrame.X = 0;
                    currentFrame.Y++;
                    if (currentFrame.Y >= 5) Die();
                }
                updateCount = 0;
            }
        }

        public void Die()
        {
            // Pre-death actions
            MainGame.BombExplosions.Remove(this);
            Game.Components.Remove(this);
        }

        // Check to see if missile has hit enemy
        public bool CollideWithEnemy(Enemy e)
        {
            Rectangle myRect = new Rectangle(
                (int)(posX - offset.X), (int)(posY - offset.Y),
                sprite.Width / 4, sprite.Height / 6);
            Rectangle enemyRect = new Rectangle(
                (int)(e.Position.X - e.Offset.X), (int)(e.Position.Y - e.Offset.Y),
                e.sprite.Width, e.sprite.Height);
            if (myRect.Intersects(enemyRect)) return true;
            else return false;
        }
    }
}